const jwt = require('jsonwebtoken');

// 简单的认证中间件
const authenticateToken = (req, res, next) => {
  // 从请求头获取token

  const authHeader = req.headers.authorization;

  const token = authHeader && authHeader.split(' ')[1]; // Bearer TOKEN
  if (!token) {
    return res.status(401).json({ message: 'Access token required' });
  }
  // 验证token
  jwt.verify(token, process.env.JWT_SECRET, (err, user) => {
    if (err) {
      return res.status(403).json({ message: 'Invalid or expired token' });
    }
    req.user = user;
    next();
  });
};

module.exports = { authenticateToken };
